<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table>
        <tr>
            <td>姓名:</td>
            <td><input type="text" placeholder="请输入姓名" class="name"></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" placeholder="请输入年龄" class="age"></td>
        </tr>
        <tr>
            <td>班级:</td>
            <td><input type="text" placeholder="请输入班级编号" class="classId"></td>
        </tr>
    </table>

    <div>
        <p>现有班级编号: 1班 1</p>
    </div>
    <div>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </div>
</body>

</html>

<script src="./config/baseUrl.js"></script>
<script src="./js/jquery-3.6.0.min.js"></script>
<script src="./js/api.js"></script>
<script>
    $(function () {
        let url = location.search
        if (url) {
            let id = url.split('?')[1].split('=')[1]
            console.log(id);
            getByid(id).then(res => {
                $('.name').val(res.data[0].name)
                $('.age').val(res.data[0].age)
                $('.classId').val(res.data[0].classId)
            })
        }
    })

    function save() {
        let url = location.search
        let obj = {
            name: $('.name').val(),
            age: $('.age').val(),
            classId: $('.classId').val()
        }
        if (url) {
            let id = url.split('?')[1].split('=')[1]
            updateStudent(id,obj).then(res=>{
                console.log(res);
            })
        } else {
            addStudent(obj).then(res => {
                console.log(res);
            })
        }
        location.href = 'index.html'
    }
    function cancel() {
        location.href = 'index.html'
    }
</script>